import threading

semaphore = threading.Semaphore(2)  # 允许最多2个线程同时访问


def do_something():
    semaphore.acquire()
    # 临界区：执行需要保护的操作
    semaphore.release()
